<?php

namespace app\api\controller\wdsxh;

use addons\wdsxh\library\Wxapp;
use app\admin\model\wdsxh\Order;
use app\admin\model\wdsxh\UserBelong;
use app\admin\model\wdsxh\MemberProfile as Model;
use think\Db;
use think\Exception;

/**
 * 入会接口
 */
class Initiation extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function field()
    {
        $uid = $this->auth->id;
        $profile = Model::where('uid', $uid)->where('status', '2')->field('content,reason')->find();
        if ($profile) {
            $profile = $profile->toArray();
            if (!empty($profile['content'])) {
                $profile['content'] = json_decode($profile['content'], true);
            }
            $this->success($profile['reason'], $profile['content']);
        } else {
            $config_file = ADDON_PATH . "wdsxh" . DS . 'config' . DS . "fieldset.php";
            if (is_file($config_file)) {
                $newMenu = include $config_file;
            }
            $this->success($newMenu);
        }
    }
    public function check()
    {
        $res = Model::where('uid', $this->auth->id)->find();
        if (empty($res)) {
            $this->success('success', array('state' => 0,'state_text' => '还未入会'));
        } elseif ($res && $res->status == '0') {
            $this->success('success', array('state' => 1,'state_text' => '入会信息正在审核'));
        } elseif ($res && $res->status == '1') {
            $member = \app\admin\model\wdsxh\Member::where('uid', $this->auth->id)->where('status', '1')->field('id,expiretime')->find();
            if (empty($member)) {
                $this->error('您还不是会员，请先申请入会');
            }

            $this->success('success', array('state' => 2,'state_text' => '已经是会员'));

//            if (strtotime($member['expiretime']) < time()) {
//                $order_where['status'] = array('in',['2','5']);
//                $orderObj = (new Order())
//                    ->where('type', 2)
//                    ->where('uid', $this->auth->id)
//                    ->where($order_where)->find();
//                if ($orderObj) {
//                    $this->success('success', array('state' => 4,'state_text' => '会员已过期,需要缴纳会费'));
//                } else {
//                    $this->success('success', array('state' => 5,'state_text' => '后台审核通过未缴费,需要缴纳会费'));
//                }
//            } else {
//                $this->success('success', array('state' => 2,'state_text' => '已经是会员'));
//            }
        } elseif ($res && $res['status'] == '2') {
            $this->success('success', array('state' => 3,'state_text' => '入会申请审核失败'));
        }
    }
    public function submit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $data = $this->request->param('data/a');
        $name = "";
        $mobile = "";
        if ($data && is_array($data)) {
            foreach ($data as $row) {
                if ($row['required'] == 1 && empty($row['value'])) {
                    if ($row['type'] == 'text' || $row['type'] == 'number' || $row['type'] == 'textarea') {
                        $this->error($row['option']);
                    }
                    if ($row['type'] == 'select' || $row['type'] == 'checkbox') {
                        $this->error($row['option'] . '至少选择一个');
                    }
                }
                if ($row['field'] == "name") {
                    $name = $row['value'];
                }
                if ($row['field'] == "mobile") {
                    $mobile = $row['value'];
                }
            }
        }
        $res = Model::where('uid', $this->auth->id)->find();
        if ($res && $res['status'] == '0') {
            $this->error('你的入会信息正在审核，不能重复提交');
        }
        if ($res && $res['status'] == '1') {
            $this->error('你已经是会员了，不能再次申请');
        }
        if ($res) {
            $res['name'] = $name;
            $res['mobile'] = $mobile;
            $res['content'] = json_encode($data);
            $res['status'] = '0';
            $res['createtime'] = time();
            $res->save();
        } else {
            Model::create(array(
                'uid' => $this->auth->id,
                'name' => $name,
                'mobile' => $mobile,
                'status' => '0',
                'content' => json_encode($data)
            ));
        }
        //发送入会申请通知
        try {
            $conf = $this->configObj;
            $data = [
                'thing2' => [
                    'value' => $name,
                ],
                'phone_number4' => [
                    'value' => $mobile,
                ],
                'phrase1' => [
                    'value' => '待审核',
                ],
            ];
            $openids = (new UserBelong())->where('set_admin', 1)->column('openid');
            if ($openids) {
                foreach ($openids as $openid) {
                    $result = Wxapp::subscribeMessage($conf['initiation_admin'], trim($openid), '', $data);
                    if ($result && $result[0]['errcode'] == 0) {
                        $id = (new UserBelong())
                            ->where('openid', $openid)
                            ->value('user_id');
                        $subscribeObj = Db::name('wdsxh_member_subscribe')->where('user_id', $id)->where('type', 1)->find();
                        if ($subscribeObj && $subscribeObj['count'] > 0) {
                            Db::name('wdsxh_member_subscribe')->where('user_id', $id)->where('type', 1)->setDec('count');
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success();
    }
}
